/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PesquisaMidias.java
 *
 * Created on 28/11/2011, 15:53:29
 */
package acervo.view;

import acervo.control.Controle;
import acervo.model.Midia;
import acervo.model.TipoMidia;
import acervo.util.AcervoTableModel;
import acervo.util.Erro;
import acervo.util.Tela;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Fernando
 */
public class PesquisaMidias extends javax.swing.JPanel {

    /** Creates new form PesquisaMidias */
    public PesquisaMidias() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPesquisa = new javax.swing.JPanel();
        labelPesquisa = new javax.swing.JLabel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 32767));
        tfPesquisa = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaPesquisa = new javax.swing.JTable();
        panelBotoes = new javax.swing.JPanel();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        botaoEditar = new javax.swing.JButton();
        filler4 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 32767));
        botaoExcluir = new javax.swing.JButton();
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));

        setLayout(null);

        panelPesquisa.setLayout(new javax.swing.BoxLayout(panelPesquisa, javax.swing.BoxLayout.X_AXIS));

        labelPesquisa.setText("Pesquisar:");
        panelPesquisa.add(labelPesquisa);
        panelPesquisa.add(filler1);

        tfPesquisa.setColumns(20);
        tfPesquisa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tfPesquisaFocusGained(evt);
            }
        });
        tfPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfPesquisaKeyTyped(evt);
            }
        });
        panelPesquisa.add(tfPesquisa);

        add(panelPesquisa);
        panelPesquisa.setBounds(20, 10, 560, 20);

        tabelaPesquisa.setModel(new AcervoTableModel());
        tabelaPesquisa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaPesquisaMouseClicked(evt);
            }
        });
        tabelaPesquisa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tabelaPesquisaFocusGained(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaPesquisa);

        DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();  

        centralizado.setHorizontalAlignment(SwingConstants.CENTER);  

        tabelaPesquisa.getColumnModel().getColumn(0).setCellRenderer(centralizado);  
        tabelaPesquisa.getColumnModel().getColumn(1).setCellRenderer(centralizado);  
        tabelaPesquisa.getColumnModel().getColumn(2).setCellRenderer(centralizado);
        tabelaPesquisa.getColumnModel().getColumn(3).setCellRenderer(centralizado);

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 40, 580, 320);

        panelBotoes.setLayout(new javax.swing.BoxLayout(panelBotoes, javax.swing.BoxLayout.LINE_AXIS));
        panelBotoes.add(filler2);

        botaoEditar.setText("Editar");
        botaoEditar.setEnabled(false);
        botaoEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoActionPerformed(evt);
            }
        });
        panelBotoes.add(botaoEditar);
        panelBotoes.add(filler4);

        botaoExcluir.setText("Excluir");
        botaoExcluir.setEnabled(false);
        botaoExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoActionPerformed(evt);
            }
        });
        panelBotoes.add(botaoExcluir);
        panelBotoes.add(filler3);

        add(panelBotoes);
        panelBotoes.setBounds(10, 370, 580, 30);
    }// </editor-fold>//GEN-END:initComponents

private void tabelaPesquisaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tabelaPesquisaFocusGained
    Tela.HabilitaCampos(botaoEditar, botaoExcluir);
}//GEN-LAST:event_tabelaPesquisaFocusGained

private void tfPesquisaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPesquisaKeyTyped
    char tecla = evt.getKeyChar();
    AcervoTableModel modelo = (AcervoTableModel) tabelaPesquisa.getModel();
    if(tecla == KeyEvent.VK_ENTER){
	ArrayList<Midia> result = new ArrayList<Midia>();
        result = Controle.obterInstancia().selectAll(tfPesquisa.getText());  
        modelo.limpar();
        modelo.addListaDeMidias(result);
    }
    
}//GEN-LAST:event_tfPesquisaKeyTyped

private void botaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoActionPerformed
    Midia midia = new Midia("");    
    AcervoTableModel modelo = (AcervoTableModel) tabelaPesquisa.getModel();
    int linha = tabelaPesquisa.getSelectedRow();
    Object id = tabelaPesquisa.getModel().getValueAt(linha, 0);
    Object tipo = tabelaPesquisa.getModel().getValueAt(linha, 2); 
    String aux = tipo.toString();      
    midia.setId(Integer.parseInt(id.toString()));
    if(aux.equals("CDAPLICATIVO"))
        midia.setTipo(TipoMidia.CDAPLICATIVO);
    else if(aux.equals("CDMUSICA"))
        midia.setTipo(TipoMidia.CDMUSICA);
    else if(aux.equals("DVDAPLICATIVO"))
        midia.setTipo(TipoMidia.DVDAPLICATIVO);
    else if(aux.equals("DVDFILME"))
        midia.setTipo(TipoMidia.DVDFILME);
    else if(aux.equals("DVDMUSICA"))
        midia.setTipo(TipoMidia.DVDMUSICA);
    else if(aux.equals("HD"))
        midia.setTipo(TipoMidia.HD);
    else if(aux.equals("LIVRO"))
        midia.setTipo(TipoMidia.LIVRO);
    else if(aux.equals("PDF"))
        midia.setTipo(TipoMidia.PDF);
    else if(aux.equals("REVISTA"))
        midia.setTipo(TipoMidia.REVISTA);
    else if(aux.equals("TEXTO"))
        midia.setTipo(TipoMidia.TEXTO);       
    midia = Controle.obterInstancia().select(midia);    
    if(evt.getSource() == botaoEditar){
        UpdateDelete janela = new UpdateDelete("Alterar", midia, "Alteração");
        janela.setSize(600, 400);        
        janela.setVisible(true);        
        janela.setLocationRelativeTo(null);        
    }
    else{
        UpdateDelete janela = new UpdateDelete("Excluir", midia, "Exclusão");
        janela.setSize(600, 400);        
        janela.setVisible(true);        
        janela.setLocationRelativeTo(null);        
    }        
    Tela.LimparCampos(tfPesquisa);
    modelo.limpar();
}//GEN-LAST:event_botaoActionPerformed

private void tfPesquisaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPesquisaFocusGained
    Tela.DesabilitaCampos(botaoEditar, botaoExcluir);
}//GEN-LAST:event_tfPesquisaFocusGained

private void tabelaPesquisaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaPesquisaMouseClicked
    if(evt.getClickCount() == 2){
        Midia midia = new Midia("");        
        AcervoTableModel modelo = (AcervoTableModel) tabelaPesquisa.getModel();
        int linha = tabelaPesquisa.getSelectedRow();
        Object id = tabelaPesquisa.getModel().getValueAt(linha, 0);
        Object tipo = tabelaPesquisa.getModel().getValueAt(linha, 2); 
        String aux = tipo.toString();        
        midia.setId(Integer.parseInt(id.toString()));
        if(aux.equals("CDAPLICATIVO"))
            midia.setTipo(TipoMidia.CDAPLICATIVO);
        else if(aux.equals("CDMUSICA"))
            midia.setTipo(TipoMidia.CDMUSICA);
        else if(aux.equals("DVDAPLICATIVO"))
            midia.setTipo(TipoMidia.DVDAPLICATIVO);
        else if(aux.equals("DVDFILME"))
            midia.setTipo(TipoMidia.DVDFILME);
        else if(aux.equals("DVDMUSICA"))
            midia.setTipo(TipoMidia.DVDMUSICA);
        else if(aux.equals("HD"))
            midia.setTipo(TipoMidia.HD);
        else if(aux.equals("LIVRO"))
            midia.setTipo(TipoMidia.LIVRO);
        else if(aux.equals("PDF"))
            midia.setTipo(TipoMidia.PDF);
        else if(aux.equals("REVISTA"))
            midia.setTipo(TipoMidia.REVISTA);
        else if(aux.equals("TEXTO"))
            midia.setTipo(TipoMidia.TEXTO);       
        midia = Controle.obterInstancia().select(midia);     
        UpdateDelete janela = new UpdateDelete("Exibição", midia, "Exibição");
        janela.setSize(600, 400);        
        janela.setVisible(true);        
        janela.setLocationRelativeTo(null);     
        Tela.LimparCampos(tfPesquisa);   
    }
}//GEN-LAST:event_tabelaPesquisaMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoEditar;
    private javax.swing.JButton botaoExcluir;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.Box.Filler filler4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelPesquisa;
    private javax.swing.JPanel panelBotoes;
    private javax.swing.JPanel panelPesquisa;
    private javax.swing.JTable tabelaPesquisa;
    private javax.swing.JTextField tfPesquisa;
    // End of variables declaration//GEN-END:variables
}
